package cn.stylefeng.guns.modular.forecastElectricityInformation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.forecastElectricityInformation.entity.QyuanPowerSdDayAheadClearedTotalEnergy;
import cn.stylefeng.guns.modular.forecastElectricityInformation.enums.QyuanPowerSdDayAheadClearedTotalEnergyExceptionEnum;
import cn.stylefeng.guns.modular.forecastElectricityInformation.mapper.QyuanPowerSdDayAheadClearedTotalEnergyMapper;
import cn.stylefeng.guns.modular.forecastElectricityInformation.pojo.request.QyuanPowerSdDayAheadClearedTotalEnergyRequest;
import cn.stylefeng.guns.modular.forecastElectricityInformation.service.QyuanPowerSdDayAheadClearedTotalEnergyService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * 日前出清电量业务实现层
 *
 * @author 我是谭先生🐻
 * @date 2025/03/14 16:23
 */
@Service
public class QyuanPowerSdDayAheadClearedTotalEnergyServiceImpl extends ServiceImpl<QyuanPowerSdDayAheadClearedTotalEnergyMapper, QyuanPowerSdDayAheadClearedTotalEnergy> implements QyuanPowerSdDayAheadClearedTotalEnergyService {

	@Override
    public void add(QyuanPowerSdDayAheadClearedTotalEnergyRequest qyuanPowerSdDayAheadClearedTotalEnergyRequest) {
        QyuanPowerSdDayAheadClearedTotalEnergy qyuanPowerSdDayAheadClearedTotalEnergy = new QyuanPowerSdDayAheadClearedTotalEnergy();
        BeanUtil.copyProperties(qyuanPowerSdDayAheadClearedTotalEnergyRequest, qyuanPowerSdDayAheadClearedTotalEnergy);
        this.save(qyuanPowerSdDayAheadClearedTotalEnergy);
    }

    @Override
    public void del(QyuanPowerSdDayAheadClearedTotalEnergyRequest qyuanPowerSdDayAheadClearedTotalEnergyRequest) {
        QyuanPowerSdDayAheadClearedTotalEnergy qyuanPowerSdDayAheadClearedTotalEnergy = this.queryQyuanPowerSdDayAheadClearedTotalEnergy(qyuanPowerSdDayAheadClearedTotalEnergyRequest);
        this.removeById(qyuanPowerSdDayAheadClearedTotalEnergy.getId());
    }

    @Override
    public void edit(QyuanPowerSdDayAheadClearedTotalEnergyRequest qyuanPowerSdDayAheadClearedTotalEnergyRequest) {
        QyuanPowerSdDayAheadClearedTotalEnergy qyuanPowerSdDayAheadClearedTotalEnergy = this.queryQyuanPowerSdDayAheadClearedTotalEnergy(qyuanPowerSdDayAheadClearedTotalEnergyRequest);
        BeanUtil.copyProperties(qyuanPowerSdDayAheadClearedTotalEnergyRequest, qyuanPowerSdDayAheadClearedTotalEnergy);
        this.updateById(qyuanPowerSdDayAheadClearedTotalEnergy);
    }

    @Override
    public QyuanPowerSdDayAheadClearedTotalEnergy detail(QyuanPowerSdDayAheadClearedTotalEnergyRequest qyuanPowerSdDayAheadClearedTotalEnergyRequest) {
        return this.queryQyuanPowerSdDayAheadClearedTotalEnergy(qyuanPowerSdDayAheadClearedTotalEnergyRequest);
    }

    @Override
    public PageResult<QyuanPowerSdDayAheadClearedTotalEnergy> findPage(QyuanPowerSdDayAheadClearedTotalEnergyRequest qyuanPowerSdDayAheadClearedTotalEnergyRequest) {
        LambdaQueryWrapper<QyuanPowerSdDayAheadClearedTotalEnergy> wrapper = createWrapper(qyuanPowerSdDayAheadClearedTotalEnergyRequest);
        Page<QyuanPowerSdDayAheadClearedTotalEnergy> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<QyuanPowerSdDayAheadClearedTotalEnergy> findList(QyuanPowerSdDayAheadClearedTotalEnergyRequest qyuanPowerSdDayAheadClearedTotalEnergyRequest) {
        LambdaQueryWrapper<QyuanPowerSdDayAheadClearedTotalEnergy> wrapper = this.createWrapper(qyuanPowerSdDayAheadClearedTotalEnergyRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 我是谭先生🐻
     * @date 2025/03/14 16:23
     */
    private QyuanPowerSdDayAheadClearedTotalEnergy queryQyuanPowerSdDayAheadClearedTotalEnergy(QyuanPowerSdDayAheadClearedTotalEnergyRequest qyuanPowerSdDayAheadClearedTotalEnergyRequest) {
        QyuanPowerSdDayAheadClearedTotalEnergy qyuanPowerSdDayAheadClearedTotalEnergy = this.getById(qyuanPowerSdDayAheadClearedTotalEnergyRequest.getId());
        if (ObjectUtil.isEmpty(qyuanPowerSdDayAheadClearedTotalEnergy)) {
            throw new ServiceException(QyuanPowerSdDayAheadClearedTotalEnergyExceptionEnum.QYUAN_POWER_SD_DAY_AHEAD_CLEARED_TOTAL_ENERGY_NOT_EXISTED);
        }
        return qyuanPowerSdDayAheadClearedTotalEnergy;
    }

    /**
     * 创建查询wrapper
     *
     * @author 我是谭先生🐻
     * @date 2025/03/14 16:23
     */
    private LambdaQueryWrapper<QyuanPowerSdDayAheadClearedTotalEnergy> createWrapper(QyuanPowerSdDayAheadClearedTotalEnergyRequest qyuanPowerSdDayAheadClearedTotalEnergyRequest) {
        LambdaQueryWrapper<QyuanPowerSdDayAheadClearedTotalEnergy> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = qyuanPowerSdDayAheadClearedTotalEnergyRequest.getId();
        String usageDate = qyuanPowerSdDayAheadClearedTotalEnergyRequest.getUsageDate();
        Integer timePoint = qyuanPowerSdDayAheadClearedTotalEnergyRequest.getTimePoint();
        String timeSlot = qyuanPowerSdDayAheadClearedTotalEnergyRequest.getTimeSlot();
        BigDecimal tradingNodeEnergy = qyuanPowerSdDayAheadClearedTotalEnergyRequest.getTradingNodeEnergy();
        BigDecimal tradingEnergy = qyuanPowerSdDayAheadClearedTotalEnergyRequest.getTradingEnergy();

        queryWrapper.eq(ObjectUtil.isNotNull(id), QyuanPowerSdDayAheadClearedTotalEnergy::getId, id);
        queryWrapper.eq(ObjectUtil.isNotNull(usageDate), QyuanPowerSdDayAheadClearedTotalEnergy::getUsageDate, usageDate);
        queryWrapper.eq(ObjectUtil.isNotNull(timePoint), QyuanPowerSdDayAheadClearedTotalEnergy::getTimePoint, timePoint);
        queryWrapper.like(ObjectUtil.isNotEmpty(timeSlot), QyuanPowerSdDayAheadClearedTotalEnergy::getTimeSlot, timeSlot);
        queryWrapper.eq(ObjectUtil.isNotNull(tradingNodeEnergy), QyuanPowerSdDayAheadClearedTotalEnergy::getTradingNodeEnergy, tradingNodeEnergy);
        queryWrapper.eq(ObjectUtil.isNotNull(tradingEnergy), QyuanPowerSdDayAheadClearedTotalEnergy::getTradingEnergy, tradingEnergy);

        return queryWrapper;
    }

}